﻿using System;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

namespace WorldMapNew
{
    // ============================== 大地图数据结构 ==============================

    /// <summary>
    /// 大地图 layer的数据结构 用于生成静态层级策略
    /// </summary>
    [Serializable]
    public class MapLayerRunDta
    {
        public int layerID;
        public string name;
        public List<int2> displayLods; // 该层级显示的lod - size (例如 树层级 只在lod1显示)

        public MapLayerRunDta()
        {
        }
    }

    /// <summary>
    /// 大地图 海外的数据结构
    /// </summary>
    [Serializable]
    public class OverSeaChunck
    {
        public int offset_x;// 相对内陆原点的偏移x
        public int offset_y;// 相对内陆原点的偏移y
        public int chunckId;// 地图块资源id

        public OverSeaInfo info;// 海外副本的数据结构 如果为null 则表示该海外地图块只是普通的海岸线装饰地图块

        public OverSeaChunck()
        {
        
        }
        public OverSeaChunck(OverSeaChunck other)
        {
            chunckId = other.chunckId;
            offset_x = other.offset_x;
            offset_y = other.offset_y;
            info = other.info;
        }
    }

    /// <summary>
    /// 大地图 海外副本的数据结构
    /// </summary>
    [Serializable]
    public class OverSeaInfo
    {
        public int overSeaType;
        public int overSeaUid;
    }

    /// <summary>
    /// 大地图 lod的数据结构
    /// </summary>
    [Serializable]
    public class MapLODData
    {
        public float lodDistance;
        public int tileSize;
        public List<int> mapChunckIds; // 对应地图块数据id 不进行旋转
        
        public List<OverSeaChunck> mapOverSeaChuncks; // 海外地图块边界id 不进行旋转

        public MapLODData()
        {
            mapChunckIds = new List<int>();
            mapOverSeaChuncks = new List<OverSeaChunck>();
        }

        public MapLODData(MapLODData other)
        {
            lodDistance = other.lodDistance;
            tileSize = other.tileSize;
            mapChunckIds = other.mapChunckIds;
            mapOverSeaChuncks = other.mapOverSeaChuncks;
        }

        public void AddMapChunckId(int chunckId)
        {
            if (mapChunckIds == null)
            {
                mapChunckIds = new List<int>();
            }
            mapChunckIds.Add(chunckId);
        }
        public void AddMapOverSeaChunck(int x,int y,int chunckId,int overSeaType,int overSeaUid)
        {
            if (mapOverSeaChuncks == null)
            {
                mapOverSeaChuncks = new List<OverSeaChunck>();
            }
            OverSeaChunck data = new OverSeaChunck();
            data.chunckId = chunckId;
            data.offset_x = x;
            data.offset_y = y;
            if (overSeaType > 0 && overSeaUid > 0)
            {
                data.info = new OverSeaInfo();
                data.info.overSeaType = overSeaType;
                data.info.overSeaUid = overSeaUid;
            }
            else
            {
                data.info = null;
            }
            mapOverSeaChuncks.Add(data);
        }
       
    }

    /// <summary>
    /// 大地图 对象的数据结构
    /// </summary>
    [Serializable]
    public class TileElement
    {
        public Vector3 position;
        public Vector3 rotation;
        public Vector3 scale;
        public int resID;
        public int pointType;// 点类型 1 主城 2 资源点 3 主城/资源点 4 联盟旗子
        public int level;// 性能等级

        public TileElement(Vector3 p, Vector3 r, Vector3 s, int resID,int pointType = 0,int level = 0)
        {
            this.position = p;
            this.position.x = (float)Math.Round(position.x, 2, MidpointRounding.AwayFromZero);
            this.position.y = (float)Math.Round(position.y, 2, MidpointRounding.AwayFromZero);
            this.position.z = (float)Math.Round(position.z, 2, MidpointRounding.AwayFromZero);
            this.rotation = r;
            this.scale = s;
            this.resID = resID;
            this.pointType = pointType;
            this.level = level;
        }

        public TileElement()
        {
        }
        public TileElement(TileElement other)
        {
            this.position = other.position;
            this.rotation = other.rotation;
            this.scale = other.scale;
            this.resID = other.resID;
            this.pointType = other.pointType;
            this.level = other.level;
        }
    }

    // ============================== 地图块数据结构 ==============================

    /// <summary>
    /// 地图块一层的数据结构
    /// </summary>
    [Serializable]
    public class MapChunckLayer
    {
        public int layerID;                     //层级id
        public string name;                     //层级名
        public int layerSize;                   //层级大小 -可以和地图块大小不一样
        public List<MapChunckElementData> elementDatas; // 层级对象数据数据

        public MapChunckLayer()
        {
            elementDatas = new List<MapChunckElementData>();
        }

        public void AddChunckElement(TileElement element, int chuncDataIdx)
        {
            if (elementDatas.Count > chuncDataIdx)
            {
                elementDatas[chuncDataIdx].AddChunckElement(element);
            }
        }

        public void SetLayerDataSize(int size)
        {
            for (int i = 0; i < size; i++)
            {
                elementDatas.Add(new MapChunckElementData());
            }
        }

    }

    /// <summary>
    /// 地图块 一块粒度的数据结构
    /// </summary>
    [Serializable]
    public class MapChunckElementData
    {
        public List<TileElement> elementDatas; // 层级对象数据数据
 
        // 街区点 出生点装饰物
        public MapChunckElementData()
        {
            elementDatas = new List<TileElement>();
        }

        public void AddChunckElement(TileElement element)
        {
            elementDatas.Add(element);
        }
    }

    // ============================== 地图块数据结构 删除 ==============================
    /// <summary>
    /// 地图块一层 删除的数据结构
    /// </summary>
    [Serializable]
    public class MapChunckLayerDeleate
    {
        public List<MapChunckElementDataDeleate> elementDeleateDatas; // 层级对象数据数据

        public MapChunckLayerDeleate()
        {
            elementDeleateDatas = new List<MapChunckElementDataDeleate>();
        }

        public void AddChunckElement(Vector2 elementLocalPos, int chuncDataIdx)
        {
            if (elementDeleateDatas.Count > chuncDataIdx)
            {
                elementDeleateDatas[chuncDataIdx].AddDeleteElementIdx(elementLocalPos);
            }
        }
    }
    /// <summary>
    /// 地图块 一块粒度 删除的数据结构
    /// </summary>
    [Serializable]
    public class MapChunckElementDataDeleate
    {
        public List<Vector2> elementDeleateDatas; // 层级对象 删除数据idx
        public MapChunckElementDataDeleate()
        {
            elementDeleateDatas = new List<Vector2>();
        }

        public void AddDeleteElementIdx(Vector2 elementLocalPos)
        {
            elementDeleateDatas.Add(elementLocalPos);
        }
    }

    // ============================== 地图迷雾数据结构 ==============================

    /// <summary>
    /// 大地图 layer的数据结构 用于生成静态层级策略
    /// </summary>
    [Serializable]
    public class MapFogRunDta
    {
        public int exploreId;// world_exploration配置键值
        public int indexFog; // 迷雾坐标系下 一维下标
        public int indexFog_x;// 迷雾坐标系下 x坐标
        public int indexFog_y;// 迷雾坐标系下 y坐标
        public int indexMap_x;// 地图坐标系下 x坐标 
        public int indexMap_y;// 地图坐标系下 y坐标

        public MapFogRunDta()
        {
        }
    }


    // ============================== 地图块数据结构 ==============================

    [Serializable]
    public class MapPointInfo
    {
        public int idx;
        public Vector3 pos;
        public MapPointInfo(int idx,Vector3 pos)
        {
            this.idx = idx;
            this.pos = pos;
        }
    }

    [Serializable]
    public class OverSeaShapeInfo
    {
        public string key; // type_uid

        public List<Vector2> points;

        public OverSeaShapeInfo(string key, List<Vector2> points)
        {
            this.key = key;
            this.points = points;
        }
    }
}